#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"
#define pf printf
int deletedirectory(char[],char[]);

int deletedirectory(char name[], char filename_g[])
{
int i,p=-1,l1,l2,e=1,c=-1,k,t,f1=0; int ud; int len;
char temp[50];
struct header h;
FILE *f;
f=fopen(filename_g,"rb+");//give exact path
fread(&h,sizeof(struct header),1,f);
fclose(f);
f=fopen(filename_g,"rb+");
rewind(f);
fwrite(&h,sizeof(struct header),1,f);
fclose(f);

len=strlen(name);
if(len==0)
{
  pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_00);
  return 0;
}

if(name[len-1]!='/') strcat(name,"/");

if ((strcmp(name,"") == 0)){
  pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_00);
  return 0;
}

if(strcmp(name,"/")==0)
{
pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_02);
return(0);
}
l2=strlen(name);
if(name[0]!='/' /*||name[l2-1]!='/'*/)
{
pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_01);
return(0);
}

for(i=1;i<TOTAL_DESC;i++)
{
if(strcmp(h.fd_arr[i].path,name)==0)
f1=1;
}

if(f1==0)
{
pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_01);
return(0);
}

//strcpy(temp,name);
//strcat(temp,"/");
for(i=0;i<TOTAL_DESC;i++)
{
if(strstr(h.fd_arr[i].path,name))
{
t=0;
c++;
l1=strlen(h.fd_arr[i].path);


for(k=0;k<l2;k++)
{
if(h.fd_arr[i].path[k]==name[k])
t++;
}

if(t==l2&&l1>l2)
{
e=0;
//pf("%s\n%s",h.fd_arr[i].path,name);
}

if((strcmp(h.fd_arr[i].path,name))==0)
{
p=i;
}

}
}


if(p>=0&&e==1)
{
strcpy(h.fd_arr[p].path,"\0");
strcpy(h.fd_arr[p].name,"\0");
strcpy(h.fd_arr[p].type,"\0");
h.fd_arr[p].size=0;
h.fd_arr[p].blockno=0;
ud=h.used_desc;
ud = ud - 1;
h.used_desc = ud;
root=del_nary(root,name);
//pf("\nDELETION SUCCESSFUL\n");
}

else if(p>=0&&e==0)
{
pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_02);
return(0);
}


else if(p==-1&&c==-1)
{
pf("deletedir_FAILURE %s\n",ERR_VFS_DELETEDIR_01);
return(0);
}




f=fopen(filename_g,"rb+");
rewind(f);
fwrite(&h,sizeof(struct header),1,f);
fclose(f);
return(1);


}


